package com.demo.component

import android.app.Activity
import android.app.AlertDialog
import android.app.Dialog
import com.base.tool.log.LogPrint

class LeakingSingleton2 private constructor() {
    private val TAG = "AgreementRequester"

    private var grantedListener: () -> Unit = {}
    private var deniedListener: () -> Unit = {}
    var dialog: Dialog? = null

    private object SingleTonHolder {
        val INSTANCE = LeakingSingleton2()
    }

    fun onGranted(listener: () -> Unit): LeakingSingleton2 {
        grantedListener = listener
        return this
    }

    fun onDenied(listener: () -> Unit): LeakingSingleton2 {
        deniedListener = listener
        return this
    }


    fun request(activity: Activity?): Boolean {


        if (dialog != null) {
            if (dialog!!.isShowing) {
                LogPrint.d(TAG, "已在展示")
                return false

            }
        }

        dialog = AlertDialog.Builder(activity)
            .setTitle("xxx")
            .setMessage("content")
            .setPositiveButton("ok", null)
            .setNegativeButton("No", null).show()

        dialog!!.show()
        return true
    }

    private fun reset() {
        dialog = null
//        grantedListener = {}
//        deniedListener = {}
    }

    companion object {
        val instance: LeakingSingleton2
            get() = SingleTonHolder.INSTANCE
    }

}

